import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },{
      path: '/findPassword',
      name: 'findPassword',
      component: () => import('../views/FindPassword.vue')
    },{
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue')
    },
    {
      path: '/admin',
      name: 'admin',
      redirect: '/userManage',
      component: () => import('../layout/adminLayout.vue'),
      children: [
        {
          path: '/userManage',
          name: 'userManage',
          component: () => import('../views/adminView/UserManage.vue')
        },{
          path: '/showManage',
          name: 'showManage',
          component: () => import('../views/adminView/ShowManage.vue')
        }
      ]
    },
    {
      path: '/user',
      name: 'user',
      redirect: '/',
      component: () => import('../layout/userLayout.vue'),
      children: [
        {
          path: '/',
          name: 'show',
          component: () => import('../views/userView/Show.vue')
        },{
          path: '/personCentre',
          name: 'personCentre',
          component: () => import('../views/userView/PersonCentre.vue')
        },{
          path: '/discussView',
          name: 'discussView',
          component: () => import('../views/userView/DiscussView.vue')
        },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // let isLogin = false
  const token = localStorage.getItem('Authorization');
  if (token || to.path === '/login' || to.path === '/findPassword' || to.path === '/register') {
    next()
  } else {
    ElMessage.warning('未登录或登录验证已过期')
    router.push('/login')
  }
})

export default router
